package com.nba.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nba.bean.Pdate;
import com.nba.bean.Tdate;
import com.nba.service.PdateService;
import com.nba.service.PlayerService;
import com.nba.service.TdateService;
import com.nba.service.TeamService;
import com.nba.service.impl.PdateServiceImpl;
import com.nba.service.impl.PlayerServiceImpl;
import com.nba.service.impl.TdateServiceImpl;
import com.nba.service.impl.TeamServiceImpl;

@WebServlet("/pdateServlet.do")
public class PdateServlet extends HttpServlet{
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}


	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String opt = request.getParameter("opt");
		switch(opt) {
			case "0":
				delete(request,response);
				break;
			case "1":
				add(request,response);
				break;
			case "2":
				deleteAll(request,response);
				break;
			case "3":
				update(request,response);
				break;
			case "4":
				break;
		}
	}
	
	public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PdateService ps = new PdateServiceImpl();
		int pdateId = Integer.parseInt(request.getParameter("pdateId"));
		ps.deletePdate(pdateId);
		response.sendRedirect("SearchServlet.do");
	}
	public void deleteAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PdateService ps = new PdateServiceImpl();
		String ids = request.getParameter("ids");
		if(ids!=null) {
			String[] idArray = ids.split("_");
			for (int i = 0; i < idArray.length; i++) {
				ps.deletePdate(Integer.parseInt(idArray[i]));
			}
		}
	}
	public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		int pdateId = Integer.parseInt(req.getParameter("pdateId"));
		int seasonId = Integer.parseInt(req.getParameter("seasonId"));
		String rteamName = req.getParameter("rteamName");
		String playerName = req.getParameter("playerName");
		int startNum = Integer.parseInt(req.getParameter("startNum"));
		double pdateTime = Double.parseDouble(req.getParameter("pdateTime"));
		double allHit = Double.parseDouble(req.getParameter("allHit"));
		double allMove = Double.parseDouble(req.getParameter("allMove"));
		double threeHit = Double.parseDouble(req.getParameter("threeHit"));
		double threeMove = Double.parseDouble(req.getParameter("threeMove"));
		double freeHit = Double.parseDouble(req.getParameter("freeHit"));
		double freeMove = Double.parseDouble(req.getParameter("freeMove"));
		double attack = Double.parseDouble(req.getParameter("attack"));
		double defeanse = Double.parseDouble(req.getParameter("defeanse"));
		double score = Double.parseDouble(req.getParameter("score"));
		double backboard = Double.parseDouble(req.getParameter("backboard"));
		double assists = Double.parseDouble(req.getParameter("assists"));
		double snatch = Double.parseDouble(req.getParameter("snatch"));
		double nutcap = Double.parseDouble(req.getParameter("nutcap"));
		double fault = Double.parseDouble(req.getParameter("fault"));
		double breakrules = Double.parseDouble(req.getParameter("breakrules"));
		String str = req.getParameter("playerDate").replace("T", " ");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date playerDate = null;
		try {
			playerDate = format.parse(str);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		TeamService ts  = new TeamServiceImpl();
		int rteamId = ts.selectTeamByName(rteamName).getTeamId();
		PlayerService ps = new PlayerServiceImpl();
		int playerId = ps.selectPlayerByName(playerName).getPlayerId();
		
		Pdate pdate = new Pdate(pdateId, playerId, playerDate, rteamId, startNum, pdateTime, allHit, allMove, threeHit, threeMove, freeHit, freeMove, attack, defeanse, score, backboard, assists, snatch, nutcap, fault, breakrules, seasonId);
		PdateService pds = new PdateServiceImpl();
		pds.updatePdate(pdate);
		resp.sendRedirect("SearchServlet.do");
	}

	public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		
		int seasonId = Integer.parseInt(req.getParameter("seasonId"));
		String rteamName = req.getParameter("rteamName");
		String playerName = req.getParameter("playerName");
		int startNum = Integer.parseInt(req.getParameter("startNum"));
		double pdateTime = Double.parseDouble(req.getParameter("pdateTime"));
		double allHit = Double.parseDouble(req.getParameter("allHit"));
		double allMove = Double.parseDouble(req.getParameter("allMove"));
		double threeHit = Double.parseDouble(req.getParameter("threeHit"));
		double threeMove = Double.parseDouble(req.getParameter("threeMove"));
		double freeHit = Double.parseDouble(req.getParameter("freeHit"));
		double freeMove = Double.parseDouble(req.getParameter("freeMove"));
		double attack = Double.parseDouble(req.getParameter("attack"));
		double defeanse = Double.parseDouble(req.getParameter("defeanse"));
		double score = Double.parseDouble(req.getParameter("score"));
		double backboard = Double.parseDouble(req.getParameter("backboard"));
		double assists = Double.parseDouble(req.getParameter("assists"));
		double snatch = Double.parseDouble(req.getParameter("snatch"));
		double nutcap = Double.parseDouble(req.getParameter("nutcap"));
		double fault = Double.parseDouble(req.getParameter("fault"));
		double breakrules = Double.parseDouble(req.getParameter("breakrules"));
		String str = req.getParameter("playerDate").replace("T", " ");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date playerDate = null;
		try {
			playerDate = format.parse(str);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		TeamService ts  = new TeamServiceImpl();
		int rteamId = ts.selectTeamByName(rteamName).getTeamId();
		PlayerService ps = new PlayerServiceImpl();
		int playerId = ps.selectPlayerByName(playerName).getPlayerId();
		
		Pdate pdate = new Pdate(playerId, playerDate, rteamId, startNum, pdateTime, allHit, allMove, threeHit, threeMove, freeHit, freeMove, attack, defeanse, score, backboard, assists, snatch, nutcap, fault, breakrules, seasonId);
		PdateService pds = new PdateServiceImpl();
		pds.insertPdate(pdate);
		resp.sendRedirect("SearchServlet.do");
	}
}

